package com.rongc.client.freight.business.mine.view.activity;

import android.view.View;
import android.widget.EditText;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.rongc.client.core.exception.RegexException;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.core.utils.RegexUtil;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.request.result.NullResult;
import com.rongc.client.freight.business.mine.request.api.FeedbackApi;
import com.rongc.client.freight.utils.UtilMethod;

import butterknife.Bind;
import butterknife.OnClick;

import static com.flyco.dialog.widget.NormalDialog.STYLE_TWO;

/**
 * Created by ccqx on 2017/7/20.
 */

public class FeedbackActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.et_content)
    EditText mEtContent;
    NormalDialog dialog;

    Response.Listener<NullResult> respOrderListener = new Response.Listener<NullResult>() {

        @Override
        public void onResponse(NullResult response) {
            if (HandleCode.requestSuccess()) {
                dialog.show();
            } else {
                UtilMethod.dismissProgressDialog(FeedbackActivity.this);
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            LogUtils.i("error.getMessage(): " + error.getMessage());
            UtilMethod.dismissProgressDialog(FeedbackActivity.this);
        }
    };

    @OnClick({R.id.btn_submit})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_submit:
                try{
                    prepare();
                    RequestManager.getInstance().call(new FeedbackApi(
                            new FeedbackApi.FeedbackParams(mEtContent.getText().toString()),
                            respOrderListener, errorListener));
                }catch (RegexException e){
                    ActivityUtils.toast(e.getMessage());
                }
                break;
        }
    }

    void prepare() throws RegexException {
        if(!RegexUtil.checkNotNull(mEtContent.getText().toString())){
            throw new RegexException("请输入反馈内容");
        }

    }

    @Override
    public void initView() {
        initToolbar(R.string.setting_feedback_lable);
        dialog = new NormalDialog(this);
        dialog.content("您的建议会帮助我们提供更好的服务。")
                .style(STYLE_TWO)
                .btnNum(1)
                .btnText(new String[]{"确定"})
                .setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.cancel();
                        finish();
                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_feedback;
    }
}
